﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace SocketServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        FileStream file;
        Socket serversocket;
        private void button1_Click(object sender, EventArgs e)
        {            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
            string test = "";
            serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint end = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse(txtPort.Text));
            serversocket.Connect(end);
            while (file.Position < file.Length)
            {
                byte[] Byte = new byte[4];
                for (int i = 0; i < 4; i++)
                    Byte[i] = (byte)file.ReadByte();

                try
                {
                    serversocket.Send(Byte);
                }
                catch (SocketException err)
                {
                    label2.Text += err.SocketErrorCode.ToString();
                }
            }
            serversocket.Close();
            textBox2.Text = test;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int Byte = file.ReadByte();
            textBox2.Text += Byte + Environment.NewLine;
        }
    }
}
